<?php
class Admin_MenuController extends Zend_Controller_Action {
	public function preDispatch() {
		$auth = Zend_Auth::getInstance ();
		if (! $auth->hasIdentity ()) {
			if ($this->_request->getActionName () != "login") {
				$this->_redirect ( "/admin/index/login" );
			}
		}
	}
	public function init() {
		/* Initialize action controller here */
		$this->_model = new Default_Model_DbTable_Menu ();
		$auth = Zend_Auth::getInstance ();
		$infoUser = $auth->getIdentity ();
		$this->view->fullName = $infoUser->USERNAME;
		Zend_Layout::startMvc ( array (
				"layoutPath" => APPLICATION_PATH . "/layouts/scripts",
				"layout" => "admin" 
		) );
	}
	public function indexAction() {
		$this->view->headTitle ( "Quản Lý Danh Mục", "PREPEND" );
		$this->view->title = "Quản Lý Danh Mục";
		$mainMenu = $this->getRequest ()->getParam ( "sort-main-menu" );
		
		$menuLVL2 = $this->getRequest ()->getParam ( "menu-lvl-2" );
		if ($mainMenu != null) {
			if ($mainMenu != "-1") {
				$menu = $this->_model->getMenuByParent ( $mainMenu, 1 );
			} else {
				$menu = $this->_model->getAllMenu ();
			}
		}
		if ($mainMenu == null || $mainMenu == "-1") {
			if ($menuLVL2 == null || $menuLVL2 == "-1")
				$menu = $this->_model->getAllMenu ();
			else{
				$menu = $this->_model->getMenuByParent ( $menuLVL2, 1 );
				$this->view->allCate1 = $menu;
			}
		}
		
		$paginator = Zend_Paginator::factory ( $menu );
		$paginator->setItemCountPerPage ( 9 );
		$paginator->setPageRange ( 5 );
		$currentPage = $this->_request->getParam ( "page", 1 );
		$paginator->setCurrentPageNumber ( $currentPage );
		$this->view->allCat = $paginator;
	}
	public function addAction() {
		$this->view->headTitle ( "Thêm Danh Mục", "PREPEND" );
		$this->view->title = "Quản Lý Danh Mục";
		$this->view->allCat = $this->_model->getAllMenu ();
		$upload = new Private_Upload ();
		if ($this->getRequest ()->isPost ()) {
			$Sub = $this->_request->getParam ( "SubID" );
			$CatName = $this->_request->getParam ( "CatName" );
			$Desc = $this->_request->getParam ( "Desc" );
			$Stt = $this->_request->getParam ( "Stt" );
			$con = $this->_request->getParam ( "Controller" );
			$img = "/ishali/public/uploaded/images/category/" . $_FILES ['Img'] ["name"];
			$upload->upload ( "category" );
			$this->_model->addMenu ( $Sub, $CatName, $Desc, $img, $con, $Stt );
			$this->_redirect ( "/admin/menu" );
		}
	}
	public function editAction() {
		$this->view->headTitle ( "Chỉnh Sửa Danh Mục", "PREPEND" );
		$this->view->title = "Quản Lý Danh Mục";
		$this->view->allCat = $this->_model->getAllMenu ();
		$upload = new Private_Upload ();
		$id = $this->_getParam ( "id" );
		$this->view->cat = $this->_model->getMenuById ( $id );
		if ($this->getRequest ()->isPost ()) {
			$Sub = $this->_request->getParam ( "SubID" );
			$CatName = $this->_request->getParam ( "CatName" );
			$Desc = $this->_request->getParam ( "Desc" );
			$Stt = $this->_request->getParam ( "Stt" );
			$con = $this->_request->getParam ( "Controller" );
			$img = "/ishali/public/uploaded/images/category/" . $_FILES ['Img'] ["name"];
			$upload->upload ( "category" );
			$this->_model->updatemenu ( $id, $Sub, $CatName, $Desc, $img, $con, $Stt );
			$this->_redirect ( "/admin/menu" );
		}
	}
	public function delAction() {
		$id = $this->_getParam ( "id" );
		$this->_model->removemenu ( $id );
		$this->_redirect ( "/admin/menu" );
	}
	public function activeAction() {
		$id = $this->_getParam ( "id" );
		$this->_model->updatestt ( $id );
		$this->_redirect ( "/admin/menu" );
	}
}